package it.uniroma3.dia.utils;

import it.uniroma3.dia.db.DBQueryConstant;
import it.uniroma3.dia.models.ErrorRatingMovie;
import it.uniroma3.dia.predictors.AbstractPredictor;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class Utils {
	public static double realUserRating(Connection connection,int userID,int movieID) throws SQLException{
		
		PreparedStatement statement = connection.prepareStatement(DBQueryConstant.SELECT_REAL_USER_RATING);
		statement.setInt(1, userID);
		statement.setInt(2, movieID);
		ResultSet res = statement.executeQuery();
		double realRating=-1;
		if(res.next())
			realRating=res.getDouble(1);
		statement.close();
		return realRating;		
	}
	
	public static ErrorRatingMovie calculateErrorMeasure(List<AbstractPredictor> predictors) {
		double totalMAE = 0;
		double totalRMSE = 0;
		int errorGreaterThan = 0;
		int total = 0;
		
		for(AbstractPredictor predictor : predictors){
			totalMAE+=predictor.getErrorMAE();
			totalRMSE+=predictor.getErrorRMSE();
			errorGreaterThan+=predictor.getErrorGreaterThanN();
			total+=predictor.getProcessedRating();
		}
		
		ErrorRatingMovie errorRatingMovie = new ErrorRatingMovie(totalMAE, totalRMSE, errorGreaterThan, total);
		return errorRatingMovie;
	}
	
}
